package com.github.tangyi.user.controller.sys;

import java.util.Map;

import cn.hutool.core.util.URLUtil;
import cn.hutool.extra.servlet.ServletUtil;
import com.github.pagehelper.PageInfo;
import com.github.tangyi.api.user.model.SysStudentStudyLog;
import com.github.tangyi.common.constant.CommonConstant;
import com.github.tangyi.user.service.sys.SysStudentStudyLogService;
import org.springframework.web.bind.annotation.*;
import com.github.tangyi.common.model.R;
import com.github.tangyi.common.utils.SysUtil;
import com.github.tangyi.common.base.BaseController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;


@Slf4j
@RestController
@AllArgsConstructor
@Tag(name = "学生学习时长记录表管理")
@RequestMapping("/v1/study/log")
public class SysStudentStudyLogController extends BaseController {

    private final SysStudentStudyLogService sysStudentStudyLogService;

    @GetMapping("/list")
	@Operation(summary = "查询学生学习时长记录表列表")
    public R<PageInfo<SysStudentStudyLog>> list(@RequestParam Map<String, Object> condition,
                                                @RequestParam(value = PAGE, required = false, defaultValue = PAGE_DEFAULT) int pageNum,
                                                @RequestParam(value = PAGE_SIZE, required = false, defaultValue = PAGE_SIZE_DEFAULT) int pageSize) {
	    return R.success(sysStudentStudyLogService.findPage(condition, pageNum, pageSize));
    }

    @GetMapping(value = "/{id}")
	@Operation(summary = "获取学生学习时长记录表详细信息")
    public R<SysStudentStudyLog> get(@PathVariable("id") Long id) {
        return R.success(sysStudentStudyLogService.get(id));
    }

    @PostMapping
	@Operation(summary = "新增学生学习时长记录表")
    public R<Boolean> add(@RequestBody @Valid SysStudentStudyLog sysStudentStudyLog, HttpServletRequest request) {
        sysStudentStudyLog.setType(1);
        sysStudentStudyLog.setIp(ServletUtil.getClientIP(request));
        sysStudentStudyLog.setUserAgent(request.getHeader("user-agent"));
        sysStudentStudyLog.setCommonValue();
        return R.success(sysStudentStudyLogService.insert(sysStudentStudyLog) > 0);
    }

    @PutMapping("{id}")
	@Operation(summary = "修改学生学习时长记录表")
    public R<Boolean> update(@PathVariable("id") Long id, @RequestBody @Valid SysStudentStudyLog sysStudentStudyLog) {
        sysStudentStudyLog.setId(id);
        sysStudentStudyLog.setCommonValue(SysUtil.getUser(), sysStudentStudyLog.getTenantCode());
        return R.success(sysStudentStudyLogService.update(sysStudentStudyLog) > 0);
    }

	@DeleteMapping("{id}")
	@Operation(summary = "删除学生学习时长记录表")
    public R<Boolean> delete(@PathVariable("id") Long id) {
        SysStudentStudyLog sysStudentStudyLog = sysStudentStudyLogService.get(id);
        sysStudentStudyLog.setCommonValue();
        return R.success(sysStudentStudyLogService.delete(sysStudentStudyLog) > 0);
    }
}
